package com.meishu.sdkdemo.adactivity.splash;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

import com.meishu.sdk.core.ad.splash.ISplashAd;
import com.meishu.sdk.core.ad.splash.ISplashFinishingTouchListener;
import com.meishu.sdk.core.ad.splash.SplashAdListener;
import com.meishu.sdk.core.ad.splash.SplashAdLoader;
import com.meishu.sdk.core.loader.AdPlatformError;
import com.meishu.sdk.core.loader.InteractionListener;
import com.meishu.sdk.core.utils.LogUtil;
import com.meishu.sdkdemo.R;
import com.meishu.sdkdemo.adactivity.MainActivity;
import com.meishu.sdkdemo.adid.IdProviderFactory;

public class SplashActivity extends AppCompatActivity implements SplashAdListener {
    private static final String TAG = "SplashActivity_";

    private ISplashAd splashAd;
    private boolean canJump = false;
    private Button btnShow;
    private Button btnSkip;
    private boolean autoShow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                | View.SYSTEM_UI_FLAG_IMMERSIVE);
        setContentView(R.layout.activity_splash);
        final ViewGroup adContainer = findViewById(R.id.splash_container);
        Log.e(TAG, "onCreate: ---"+adContainer.getContext() );
        String pid  = getIntent().getStringExtra("alternativePlaceId");

        if (TextUtils.isEmpty(pid)) {
            pid = IdProviderFactory.getDefaultProvider().splash();
        }

        btnShow = findViewById(R.id.btn_show);
        btnSkip = findViewById(R.id.btn_skip);
        SplashAdLoader splashAdLoader;
        Integer id = getIntent().getIntExtra("id", -1);
        switch (id) {
            case R.id.loadAndShowSplashAd:
                splashAdLoader = new SplashAdLoader(this, adContainer, pid, SplashActivity.this, 3000);
                splashAdLoader.hideSkipBtn(false);
//                splashAdLoader.setAdSize(640,960);
                splashAdLoader.loadAd();
                autoShow = true;
                break;
            case R.id.loadAndShowShakeSplashAd:
                splashAdLoader = new SplashAdLoader(this, adContainer, "73646b0000000002", SplashActivity.this, 3000);
                splashAdLoader.loadAd();
                autoShow = true;
                break;
            case R.id.loadSplashAd:
                splashAdLoader = new SplashAdLoader(this, pid, SplashActivity.this, 3000*60);
                btnShow.setVisibility(View.VISIBLE);
//                splashAdLoader.hideSkipBtn(true);
                splashAdLoader.loadAdOnly();
                autoShow = false;
                break;
            case R.id.customSkipSplashAd:
                splashAdLoader = new SplashAdLoader(this, adContainer, pid, SplashActivity.this, 3000);
                btnSkip.setVisibility(View.VISIBLE);
                splashAdLoader.loadAd(btnSkip);
                autoShow = true;
                break;
            default:
                throw new IllegalStateException("Unexpected value: " + id);
        }

        findViewById(R.id.btn_show).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (splashAd != null) {
                    splashAd.showAd(adContainer);
                }
            }
        });
    }

    /**
     * 注意：广告加载成功不要再做原有逻辑处理，应该转移到onAdReady中
     * 广告加载成功，此时广告不一定会渲染成功，这里不做任何处理，所有处理统一放到onAdReady中
     */
    @Override
    public void onAdLoaded(ISplashAd splashAd) {
        Log.d(TAG, "DEMO ADEVENT " + (new Throwable().getStackTrace()[0].getMethodName()));
//        if (splashAd.getData()!=null){
//            Toast.makeText(this,splashAd.getData().toString(),Toast.LENGTH_LONG).show();
//            AdSdk.adConfig().userDenyCats(splashAd.getData().getCat());
//            AdSdk.adConfig().userDenyCids(splashAd.getData().getCid());
//            AdSdk.adConfig().userDenyAderIds(splashAd.getData().getAderId());
//        }


    }

    @Override
    public void onAdExposure() {
        Log.d(TAG, "DEMO ADEVENT " + (new Throwable().getStackTrace()[0].getMethodName()));
    }

    @Override
    public void onAdClosed() {
        Log.d(TAG, "DEMO ADEVENT " + (new Throwable().getStackTrace()[0].getMethodName()));

        next();

    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d(TAG, "DEMO ADEVENT " + (new Throwable().getStackTrace()[0].getMethodName()));
        canJump = false;
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG, "DEMO ADEVENT " + (new Throwable().getStackTrace()[0].getMethodName()));
        if (this.splashAd != null && canJump) {
            next();
        }
        canJump = true;
    }

    private void next() {
        if (canJump){
            // 此处打开需要的 activity 即可
            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
            this.finish();
        }else {
            canJump = true;
        }

    }



    @Override
    public void onAdError() {
        Log.d(TAG, "DEMO ADEVENT " + (new Throwable().getStackTrace()[0].getMethodName()));
        Toast.makeText(this.getApplicationContext(), "没有加载到广告", Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        this.finish();
    }

    @Override
    public void onAdPlatformError(AdPlatformError e) {
        Log.d(TAG, "DEMO ADEVENT " + (new Throwable().getStackTrace()[0].getMethodName()) + " " + e);
    }

    /**
     * 广告已经准备完毕，此时可以进行广告的展示操作和相关逻辑处理
     */
    @Override
    public void onAdReady(ISplashAd splashAd) {
        Log.e(TAG, "onRenderSuccess: "+splashAd);
        if (splashAd==null) return;
        this.splashAd = splashAd;
        if (!autoShow) {
            btnShow.setEnabled(true);
        }
        SplashFinishingTouchManager.getInstance().setSplashInfo(splashAd,splashAd.getAdView());
        SplashFinishingTouchManager.getInstance().setSupportSplashFinishingTouch(false);
        splashAd.setSplashFinishingTouchListener(new ISplashFinishingTouchListener() {
            @Override
            public void onSplashAnimationStart() {

            }

            @Override
            public void onSplashAnimationFinish() {

            }

            @Override
            public void isSupportSplashClickEye(boolean isSupport) {
                LogUtil.e(TAG, "isSupportSplashClickEye: "+isSupport );
                SplashFinishingTouchManager.getInstance().setSupportSplashFinishingTouch(isSupport);
            }
        });
        splashAd.setInteractionListener(new InteractionListener() {

            @Override
            public void onAdClicked() {
                Log.d(TAG, "DEMO ADEVENT " + (new Throwable().getStackTrace()[0].getMethodName()));
            }
        });
    }

    @Override
    public void onAdRenderFail(String msg, int code) {

    }


    @Override
    public void onAdPresent(ISplashAd splashAd) {
        Log.d(TAG, "DEMO ADEVENT " + (new Throwable().getStackTrace()[0].getMethodName()));
    }

    @Override
    public void onAdSkip(ISplashAd splashAd) {
        Log.d(TAG, "DEMO ADEVENT " + (new Throwable().getStackTrace()[0].getMethodName()));
    }

    @Override
    public void onAdTimeOver(ISplashAd splashAd) {
        // 仅支持msad和穿山甲，倒计时结束时回调
        Log.d(TAG, "DEMO ADEVENT " + (new Throwable().getStackTrace()[0].getMethodName()));
    }

    @Override
    public void onAdTick(long leftMilliseconds) {
        // 仅支持msad和广点通，回调剩余时间
        Log.d(TAG, "DEMO ADEVENT " + (new Throwable().getStackTrace()[0].getMethodName()) + " " + leftMilliseconds);
        btnSkip.setText(leftMilliseconds + "");
    }


}
